//
//  PywPayBottomView.m
//  PywSdk
//
//  Created by Jade on 16/11/16.
//  Copyright © 2016年 Jade. All rights reserved.
//

#import "PywPayBottomView.h"
#import "PywMasonry.h"

@interface PywPayBottomView()
@property (nonatomic,strong) UILabel *moneyLabel;
@property (nonatomic,strong) UIButton *buyButton;

@property (nonatomic,copy) SelectBuyFinish selectBuyFinish;
@property (nonatomic,assign) CGFloat money;
@end

@implementation PywPayBottomView

- (instancetype)initWithMoney:(CGFloat )money
                       Finish:(SelectBuyFinish)buyFinish
{
    self = [super init];
    if (self) {
        self.money = money;
        self.selectBuyFinish = buyFinish;
        [self setUp];
    }
    return self;
}

#pragma mark - setUp
- (void)setUp
{
    [self addSubview:self.moneyLabel];
    [self addSubview:self.buyButton];
    [self addConstraints];
    self.backgroundColor = [UIColor whiteColor];
}

- (void)addConstraints
{
    WEAK_SELF;
    [self.moneyLabel mas_makeConstraints:^(PywMASConstraintMaker *make) {
        STRONG_SELF(weakSelf);
        make.left.bottom.offset(0);
        make.height.equalTo(@50);
        make.right.equalTo(strongSelf.buyButton.mas_left);
    }];
    
    [self.buyButton mas_makeConstraints:^(PywMASConstraintMaker *make) {
        STRONG_SELF(weakSelf);
        make.bottom.top.equalTo(strongSelf.moneyLabel);
        make.right.offset(0);
        make.width.equalTo(@100);
    }];
}

- (UILabel *)moneyLabel
{
    if (!_moneyLabel) {
        _moneyLabel = [[UILabel alloc] init];
        _moneyLabel.textColor = [UIColor blackColor];
        _moneyLabel.font = [UIFont systemFontOfSize:15.f];
        NSString *needPayAount = [NSString stringWithFormat:@"%.2f",self.money];
        NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"     支付金额：￥%@", needPayAount]];
        [attStr addAttribute:NSForegroundColorAttributeName value:RGB(250, 65, 72, 1) range:NSMakeRange(10, attStr.length - 10)];
        _moneyLabel.attributedText = attStr;
    }
    return _moneyLabel;
    
}

- (UIButton *)buyButton
{
    if (!_buyButton) {
        _buyButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_buyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_buyButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
        [_buyButton setTitle:@"立即购买" forState:UIControlStateNormal];
        [_buyButton addTarget:self
                       action:@selector(buyBtnClick:)
             forControlEvents:UIControlEventTouchUpInside];
        _buyButton.titleLabel.font = [UIFont systemFontOfSize:15];
        _buyButton.backgroundColor = [UIColor orangeColor];
        [_buyButton setAccessibilityIdentifier:@"recharge_buy"];
    }
    return _buyButton;
}


#pragma mark - Actions
/**
 *  点击支付
 *
 *  @param sender <#sender description#>
 */
- (void)buyBtnClick:(UIButton *)sender
{
    if (self.selectBuyFinish) {
        self.selectBuyFinish();
    }
}

/**
 *  刷新支付金额
 *
 *  @param payMoney <#payMoney description#>
 */
- (void)reloadPayMoney:(CGFloat )payMoney
{
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"     支付金额：￥%@", [NSString stringWithFormat:@"%.2f", payMoney]]];
    [attStr addAttribute:NSForegroundColorAttributeName value:RGB(250, 65, 72, 1) range:NSMakeRange(10, attStr.length - 10)];
    self.moneyLabel.attributedText = attStr;
}
#pragma mark - Public
/**
 *  修改支付按钮是否可以点击
 */
-(void)switchPayStuatus:(BOOL)isEnabled
{
    self.buyButton.enabled = isEnabled;
}
@end
